#include "process.h"
#include<unistd.h>
#include<stdlib.h>
#include<time.h>


//定义函数指针类型
typedef void (*call_t)(const char*,double,double);

double total =1024.0;//下载总量
//double speed =1.0;//下载速度
double speed[]={10, 0 , 0.002 , 0.012 , 0.0231 , 0.2143};


//回调函数
void download(double total,call_t cb)
{
    srand(time(NULL));
    double current=0.0;
    while(1)
    {
        //进行回调
        cb("下载中",total,current);//total:总量    current:已下载量
        
        if(total<=current)break;
        //模拟下载
        int random=rand()%6;
        current+=speed[random];
        usleep(5000);
        if(current>=total){current=total;};
    }
}


void Upload(double total,call_t cb)
{
    srand(time(NULL));
    double current=0.0;
    while(1)
    {
        //进行回调
        cb("上传中",total,current);//total:总量    current:已下载量
        
        if(total<=current)break;
        //模拟上传
        int random=rand()%6;
        current+=speed[random];
        usleep(5000);
        if(current>=total){current=total;};
    }
}


int main()
{
    
    download(1024,FlsuhProcess);
    printf("Download: 1024.0MB done\n");
    download(666,FlsuhProcess);
    printf("Download: 666.0MB done\n");
    download(412,FlsuhProcess);
    printf("Download: 412.0MB done\n");
    download(432,FlsuhProcess);
    printf("Download: 432.0MB done\n");
    download(1234,FlsuhProcess);
    printf("Download: 1234.0MB done\n");
    //process();
    Upload(2132,FlsuhProcess);
    printf("Upload: 1234.0MB done\n");
    //process();

    return 0;

}
